<script>
	import { mapActions, mapGetters } from 'vuex'
	import { requestURL } from '@/common/utils/config'
	import Vue from 'vue'	
	export default {
		onLaunch: function () {
			uni.removeStorageSync('launchFlag')
			// #ifdef APP-PLUS
			uni.setStorageSync('shop', 0)
			// #endif
			this.getSysInfo()
			this.getHome()
			// #ifdef MP-WEIXIN
			this.checkLogin()
			// #endif		
		},
		methods: {
			...mapActions([
				'getModel',
				'GetAppid'
			]),
			// #ifdef MP-WEIXIN
			checkLogin () {
				uni.checkSession({
					success: res => {
						console.error('登录状态未过期')
					},
					fail: err => {
						console.error('登录状态已过期')
						this.$store.commit('SET_USERINFO', 0)
					}
				})
			},
			// #endif
			async getHome () { // 获取模板
				uni.showLoading()
				const userInfo = uni.getStorageSync('userInfo') // 获取localstorage用户信息
				const address = uni.getStorageSync('address')// 获取localstorage用户地址信息
				let shop = uni.getStorageSync('shop') // 获取店铺信息
				if (userInfo) this.$store.commit('SET_USERINFO', userInfo)
				if (address) this.$store.commit('SET_ADDRESS', address)				
				await this.getModel(this.$appid)
				if (this.$appid !== shop.appid) shop = 0
				shop ? this.$store.commit('SET_SHOP', shop) : await this.GetAppid(this.$appid)
				// #ifdef APP-PLUS
				await this.GetAppid(this.$appid)
				await this.update()
				// #endif				
				uni.hideLoading()
			},
			getSysInfo () {
				uni.getSystemInfo({
					success: function(e) {
						// #ifndef MP
						Vue.prototype.StatusBar = e.statusBarHeight;
						if (e.platform == 'android') {
							Vue.prototype.CustomBar = e.statusBarHeight + 50;
						} else {
							Vue.prototype.CustomBar = e.statusBarHeight + 45;
						};
						// #endif

						// #ifdef MP-WEIXIN
						Vue.prototype.StatusBar = e.statusBarHeight;
						let custom = wx.getMenuButtonBoundingClientRect();
						Vue.prototype.Custom = custom;
						Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
						// #endif		

						// #ifdef MP-ALIPAY
						Vue.prototype.StatusBar = e.statusBarHeight;
						Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
						// #endif
					}
				})				
			},
			update () {
				return new Promise(resolve => {
					let server = `${requestURL}/smallroutine/program/updataver`
					let	req = {
						customer_id: this.shop.customer_id,
						version: plus.runtime.version
					}
					uni.request({
						url: server,
						data: req,
						method: 'post',
						success: res => {
							resolve()							
							if (!res.data.code) {
								this.$store.commit('SET_ISUPDATE', true)
								uni.showModal({
									title: "更新提示",
									content: res.data.rsp.note,
									confirmText: '更新',
									showCancel: false,
									success: rst => {
										if (rst.confirm) {
											uni.downloadFile({
												url:  res.data.rsp.url,
												success: files => {
													if (files.statusCode === 200) {
														plus.runtime.install(files.tempFilePath, {}, () => {
															uni.showToast({
																title: '更新成功',
																icon: 'none'
															})
														})
													}
												},
												fail: err => {
													plus.nativeUI.alert('下载更新文件失败')
												}
											})
										} else {
											uni.showModal({
												title: '提示',
												content: '因为您没有更新新版本，所以有些功能不能使用!',
												showCancel: false,
												success: rst => {
													if (rst.cancel) {
														this.$store.commit('SET_ISUPDATE', true)
													}
												}												
											})											
										}
									}
								})
							}
						}
					})					
				})
			}
		},
		computed: {
			...mapGetters([
				'shop'
			])
		}
	}
</script>

<style lang="less">
	/*每个页面公共css */
	@import './common/styles/variables.less';
	@import './common/styles/animation.css';
	@import './common/styles/main.css';
	@import './common/styles/icon.css';
	page, uni-page-body {
		background-color:#f4f4f4;
	}
	view, text {
		font-size:@medium;
	}
	progress, checkbox-group{
		width: 100%;
	}	
</style>
